package com.hansming.bss.modules.sfc.util;

import java.security.MessageDigest;
import java.util.regex.Pattern;

/*
 * 正则验证手机号码和邮箱   若是手机返回1 ，  邮箱返回2   ， 若都不是返回 0 
 * 检测是否能够
 * 对字符串进行MD5编码
 * 
 */
public class AccountsCheckUtil {
	
	private final static String[] hexDigits = { "0", "1", "2", "3", "4", "5",
	            "6", "7", "8", "9", "a", "b", "c", "d", "e", "f" };
	
	
	/**
     * 正则表达式：验证手机号
     */
    public static final String REGEX_MOBILE = "^((17[0-9])|(14[0-9])|(13[0-9])|(15[^4,\\D])|(18[0,5-9]))\\d{8}$";
 
    /**
     * 正则表达式：验证邮箱
     */
    public static final String REGEX_EMAIL = "^([a-z0-9A-Z]+[-|\\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$";
	
	
	//验证帐号如果格式是手机帐号返回1 ，格式符合邮箱格式返回2，若2种都不适合则返回0
	public static int checkaccount(String account){
		int result = 0;
		if(Pattern.matches(REGEX_MOBILE,account)){
			result = 1;
		}
		if(Pattern.matches(REGEX_EMAIL,account)){
			result = 2;
		}
		return result;
	}
	
	
	
	

	/**
	 * 对字符串进行MD5编码
	 * @param originString
	 * @return
	 */
	public static String encodeByMD5(String originString) {
	        if (originString != null){
	            try {
	                MessageDigest md = MessageDigest.getInstance("MD5");
	                byte[] results = md.digest(originString .getBytes());
	                String resultString = byteArrayToHexString(results);
	                return resultString.toUpperCase();
	            } catch (Exception ex) {
	                ex.printStackTrace();
	            }
	        }
	        return null;
	    }

	    /**
	     * 转换字节数组为16进制字串
	     * 
	     * @param b  字节数组
	     * @return 十六进制字串
	     */
	    private static String byteArrayToHexString(byte[] b) {
	        StringBuffer resultSb = new StringBuffer();
	        for (int i = 0; i < b.length; i++) {
	            resultSb.append(byteToHexString(b[i]));
	        }
	        return resultSb.toString();
	    }

	    /**
	     * 将一个字节转化成16进制形式的字符串
	     * @param b
	     * @return
	     */
	    private static String byteToHexString(byte b) {
	        int n = b;
	        if (n < 0)
	            n = 256 + n;
	        int d1 = n / 16;
	        int d2 = n % 16;
	        return hexDigits[d1] + hexDigits[d2];
	    }
}
